<?php
	$xtpl_main->assign ( "page_title", "Home" );
	
	//--
	if(isset($_POST['frmAct']) && $_POST['frmAct'] == 'doReport'){
		$_POST['task_description'] = str_replace("\r", '', $_POST['task_description']);
		$_POST['task_description'] = preg_replace('/(?<!>)\n/', "<br />\n", $_POST['task_description']);
		if($_POST['frmTaskId'] == ''){
			mysql_query("
							INSERT INTO task 
								(project_id, task_assigned_to, task_created_date, task_description) 
							VALUES 
								('".$_POST['project_id']."', '".$_SESSION['onlineMemberId']."', '".date('Y-m-d')."', '".$_POST['task_description']."')
						");
			
			$taskId = mysql_insert_id();
			
			mysql_query("
							INSERT INTO report 
								(member_id, project_id, task_id, report_time, report_created_date) 
							VALUES 
								('".$_SESSION['onlineMemberId']."', '".$_POST['project_id']."', '".$taskId."', '".$_POST['task_time']."', '".date('Y-m-d')."')
						");
		}else{
			
			mysql_query("
							UPDATE task 
							SET 
								project_id = '".$_POST['project_id']."',
								task_description = '".$_POST['task_description']."' 
							WHERE 
								task_id = '".$_POST['frmTaskId']."' AND task_assigned_to = '".$_SESSION['onlineMemberId']."' 
							LIMIT 1
						");
			mysql_query("
							UPDATE report 
							SET 
								project_id = '".$_POST['project_id']."', 
								report_time = '".$_POST['task_time']."' 
							WHERE 
								task_id = '".$_POST['frmTaskId']."' AND member_id = '".$_SESSION['onlineMemberId']."'
						");
		}
		header("Location: index.php");
	}
	
	//-- Select project
	if(isset($_GET['t']) && $_GET['t'] != ''){
		$rsSelectedProjectId = mysql_query("SELECT t.project_id, t.task_description, r.report_time, t.task_id FROM task t INNER JOIN report r ON r.task_id = t.task_id WHERE t.task_id = '".$_GET['t']."' AND t.task_assigned_to = '".$_SESSION['onlineMemberId']."'");
		if(mysql_num_rows($rsSelectedProjectId) > 0){
			$rowSelectedProjectId = mysql_fetch_array($rsSelectedProjectId);
			$selectedProjectId = $rowSelectedProjectId['project_id'];
			
			$xtpl->assign("editReportDescription", $rowSelectedProjectId['task_description']);
			$xtpl->assign("editReportTime", $rowSelectedProjectId['report_time']);
			$xtpl->assign("TaskId", $rowSelectedProjectId['task_id']);
		}
	}
	
	//-- List select option project
	$rsSelectProject = mysql_query("SELECT project_id, project_name FROM project WHERE 1 ORDER BY project_id DESC");
	while($rowSelectProject = mysql_fetch_array($rsSelectProject)){
		if(isset($selectedProjectId) && $selectedProjectId != '' && $rowSelectProject['project_id'] == $selectedProjectId) $rowSelectProject['project_selected'] = 'selected="selected"';
		$xtpl->assign("listSelectProject", $rowSelectProject);
		$xtpl->parse("center.listSelectProject");
	}
	
	//-- List project with task
	$rsListProjectSum = mysql_query("SELECT project_id, project_name FROM project WHERE 1 ORDER BY project_id DESC");
	while($rowListProjectSum = mysql_fetch_array($rsListProjectSum)){
		//-- Count task
		$rsCountTask = mysql_query("SELECT task_id FROM task WHERE project_id = '".$rowListProjectSum['project_id']."'");
		$countTask = mysql_num_rows($rsCountTask);
		$rowListProjectSum['projectTaskLink'] = BASE_URL . '?act=project&id=' . $rowListProjectSum['project_id'];
		$rowListProjectSum['projectTaskLink'] = '<a href="'.$rowListProjectSum['projectTaskLink'].'">'.$countTask.' tasks</a>';
		
		//-- Count feedback
		$rsCountFeedback = mysql_query("SELECT feedback_id FROM feedback WHERE project_id = '".$rowListProjectSum['project_id']."'");
		$countFeedback = mysql_num_rows($rsCountFeedback);
		$rowListProjectSum['projectFeedbackLink'] = BASE_URL . '?act=feedback&id=' . $rowListProjectSum['project_id'];
		$rowListProjectSum['projectFeedbackLink'] = '<a href="'.$rowListProjectSum['projectFeedbackLink'].'">'.$countFeedback.' feedbacks</a>';
		
		$xtpl->assign("ListProjectSum", $rowListProjectSum);
		$xtpl->parse("center.ListProjectSum");
	}
?>
